﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace proj3
{
    public partial class Delform : Form
    {
        private int num;
        public Delform(int idx=0)
        {
            num = idx;
            InitializeComponent();
            textBox.Text = "Do you want to delete infomation of " + Program.nameList[idx].name + "?";
        }

        private void yesbtn_Click(object sender, EventArgs e)
        {
            //delete from namelist
           Program.nameList.RemoveAt(num);

            //delete from data
            int counter = 0;
            string line;
            // Read the file and display it line by line.
            StreamWriter sw = new StreamWriter("tmp.txt", false);
            System.IO.StreamReader file = new System.IO.StreamReader("data.txt");
            while ((line = file.ReadLine()) != null)
            {
                if (counter != num)
                    sw.WriteLine(line);
                counter++;
            }
            sw.Close();
            file.Close();
            this.Close();
            File.Delete("data.txt");
            File.Move("tmp.txt", "data.txt");
            Console.WriteLine(Program.nameList.Count + "DEBUG");
        }

        private void nobtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Delform_FormClosed(object sender, FormClosedEventArgs e)
        {
         
        }
    }
}
